/**
 *
 */
package co.fitstart.weapp.api.web.controller.content;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;

import co.fitstart.entity.content.Article;
import co.fitstart.entity.content.Module;
import co.fitstart.weapp.api.service.ContentService;

/**
 * @author downpour
 *
 */
@RestController
@RequestMapping("")
public class ArticleApiController {

    @Autowired
    private ContentService contentService;
    
    private Logger logger = LoggerFactory.getLogger(ArticleApiController.class);
    
    /**
     * 
     * @param perPage
     * @return
     */
    @RequestMapping(path = "/articles", method = RequestMethod.GET, params="perPage")
    public HttpResult list(@RequestParam("perPage") int perPage) {
        HttpResult result = new HttpResult();
        
        try {
            
            List<Article> articles = contentService.listArticle(perPage);
            result.success().data("articles", articles);
            
        } catch (Exception e) {
            result.fail("500").message("获取数据失败");
            logger.error("list - ", e);
        }

        return result;
    }

    /**
     * List all articles
     *
     * @param keyword
     * @param moduleId
     * @param page
     * @return
     */
    @RequestMapping(value = "/articles", method = RequestMethod.GET)
    public HttpResult list(@RequestParam(value = "q", required = false) String keyword,
                           @RequestParam(value = "m", required = false) Long moduleId,
                           Page page) {

        HttpResult result = new HttpResult();

        int totals = contentService.countArticle(moduleId, keyword);
        
        if(totals > 0) {
            List<Article> articles = contentService.listArticle(moduleId, keyword, page.with(totals));
            
            result.success()
                  .data("page", page)
                  .data("articles", articles);
        }

        if(StringUtils.isNotEmpty(keyword)) {
            result.data("q", keyword);
        }
        if(moduleId != null) {
            Module module = contentService.getModule(moduleId);
            result.data("m", moduleId)
                  .data("moduleName", module == null ? "" : module.getName());
        }

        return result;
    }

    /**
     *
     * @return
     */
    @RequestMapping(value = "/modules", method = RequestMethod.GET)
    public HttpResult modules() {
        HttpResult result = new HttpResult();
        List<Module> modules = contentService.listModules();
        result.success().data("modules", modules);
        return result;
    }

}
